package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"rongyy/src/models"
	"rongyy/src/server"
	"rongyy/src/service"
	"time"
)

type StatisController struct {
	*service.StatisService
}

func NewStatisController() *StatisController {
	return &StatisController{}
}

func (rc *StatisController) Test(ctx *gin.Context) string {
	log.Println(465456465)
	return "bosuai"
}

func (qc *StatisController) regStatis(c *gin.Context) server.IModel {
	json := &models.StatisRegReq{}
	err := c.ShouldBindJSON(json)
	if err != nil {
		return &models.DataModel{IsSuccess: false, Msg: "统计挂号参数错误"}
	}
	fmt.Printf(json.DeptName)
	data, err := qc.RegAll(json.Begin, json.End, json.DeptName)
	return &models.DataModel{IsSuccess: true, Msg: "查询成功", Data: data}
}
func (qc *StatisController) patientStatis(c *gin.Context) server.IModel {
	json := &models.StatisRegReq{}
	err := c.ShouldBindJSON(json)
	if err != nil {
		return &models.DataModel{IsSuccess: false, Msg: "建卡统计参数错误"}
	}
	data, err := qc.PatAll(json.Begin, json.End, json.DeptName)
	return &models.DataModel{IsSuccess: true, Msg: "查询成功", Data: data}
}

func (qc *StatisController) statisTotal(c *gin.Context) server.IModel {
	json := &models.StatisRegReq{}
	err := c.ShouldBindJSON(json)
	if err != nil {
		return &models.DataModel{IsSuccess: false, Msg: "建卡统计参数错误"}
	}
	now := time.Now()
	regTotal, err := qc.RegTotals(json.Begin, json.End)
	opTotal, err := qc.OpTotals(json.Begin, json.End)
	cardTotal, err := qc.CardTotals(json.Begin, json.End)

	firstOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()).Format("2006-01-02")
	lastOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()).AddDate(0, 1, -1).Format("2006-01-02")
	fmt.Println(firstOfMonth)
	fmt.Println(lastOfMonth)
	regMonth, err := qc.RegTotals(firstOfMonth, lastOfMonth)
	opMonth, err := qc.OpTotals(firstOfMonth, lastOfMonth)
	cardMonth, err := qc.CardTotals(firstOfMonth, lastOfMonth)

	firstOfLastMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()).AddDate(0, -1, 0).Format("2006-01-02")
	lastOfLastMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()).AddDate(0, 0, -1).Format("2006-01-02")
	fmt.Println(firstOfLastMonth)
	fmt.Println(lastOfLastMonth)
	regLastMonth, err := qc.RegTotals(firstOfLastMonth, lastOfLastMonth)
	opLastMonth, err := qc.OpTotals(firstOfLastMonth, lastOfLastMonth)
	cardLastMonth, err := qc.CardTotals(firstOfLastMonth, lastOfLastMonth)
	return &models.DataModel{IsSuccess: true, Msg: "查询成功", Data: map[string]interface{}{
		"reg":           regTotal,
		"op":            opTotal,
		"card":          cardTotal,
		"regMonth":      regMonth,
		"opMonth":       opMonth,
		"cardMonth":     cardMonth,
		"regLastMonth":  regLastMonth,
		"opLastMonth":   opLastMonth,
		"cardLastMonth": cardLastMonth,
	}}
}

func (this *StatisController) RouterHandler(server *server.Server) {
	server.Handle("POST", "/reg-all", this.regStatis)
	server.Handle("POST", "/pat-all", this.patientStatis)
	server.Handle("POST", "/totals-all", this.statisTotal)

	//server.Handle("POST", "/relation/create", this.createRelationNew)
	//server.Handle("POST", "/findTodayRecord", this.FindTodayRecord)
	//server.Handle("POST", "/relationFindTodayRecord", this.relationFindTodayRecord)
}
